package com.badlogic.androidgames.framework.impl;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @deprecated:快速渲染视图
 * @author frank
 * @date 2015年11月29日
 */
public class AndroidFastRenderView extends SurfaceView implements Runnable {
	//SurfaceView 平面视图	Runnable 线程
	
	AndroidGame game;
	/**位图 framebuffer 帧缓冲**/
	Bitmap framebuffer;
	/**线程 renderThread 渲染线程**/
	Thread renderThread = null;
	/**平面支架**/
	SurfaceHolder holder;
	volatile boolean running = false;
	
	public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) {
		super(game);
		this.game = game;
		this.framebuffer = framebuffer;
		this.holder = getHolder();
	}
	/**重新开始**/
	public void resume() {
		running = true;
		renderThread = new Thread(this);
		renderThread.start();
	}
	
	@Override
	public void run() {
		Rect dstRect = new Rect();	// Rect 矩形
		long startTime = System.nanoTime();	// 随机时间毫秒
		while (running) {
			if (!holder.getSurface().isValid())	// Valid 有效
				continue;
			float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
			startTime = System.nanoTime();
			
			game.getCurrentScreen().update(deltaTime);	// Current Screen 当前屏幕
			game.getCurrentScreen().present(deltaTime);	// present 现在
			/**Canvas 画布**/
			Canvas canvas = holder.lockCanvas();	// lock 锁定
			canvas.getClipBounds(dstRect);	// Clip Bounds 剪辑边界
			canvas.drawBitmap(framebuffer, null, dstRect, null);	// draw 画
			holder.unlockCanvasAndPost(canvas);	// 解锁画布和布置
		}
	}
	/**暂停**/
	public void pause() {
		running = false;
		while (true) {
			try {
				renderThread.join();	//加入
				break;
			} catch (InterruptedException e) {
				// retry 重试
			}
		}
	}

}
